package com.sunwayworld.basemodule.coredata.method.service.impl;

import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.utils.NumberUtils;
import org.apache.cxf.common.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.method.dao.LimsMethodCertificateDao;
import com.sunwayworld.basemodule.coredata.method.bean.LimsMethodCertificateBean;
import com.sunwayworld.basemodule.coredata.method.service.LimsMethodCertificateService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsMethodCertificateServiceImpl implements LimsMethodCertificateService {

    @Autowired
    private LimsMethodCertificateDao limsMethodCertificateDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsMethodCertificateDao getDao() {
        return limsMethodCertificateDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsMethodCertificateBean limsMethodCertificate = jsonWrapper.parseUnique(LimsMethodCertificateBean.class);
        Long personId=limsMethodCertificate.getPersonId();
        String methodIds=limsMethodCertificate.getExt$Item("methodids");
        if(methodIds==null){
            return 0L;
        }
        List<Long> methodIdList = Arrays.stream(methodIds.split(",")).map(NumberUtils::parseLong).collect(Collectors.toList());
        List<LimsMethodCertificateBean> certificateBeans=selectListByFilter(
                SearchFilter.instance().match("methodId",methodIdList).filter(MatchPattern.OR)
        .match("personId",personId).filter(MatchPattern.EQ));
        List<LimsMethodCertificateBean> certificates=new ArrayList<>();
        if(certificateBeans.isEmpty()){//不存在不相同的
            methodIdList.stream().forEach(d ->{
                LimsMethodCertificateBean certificateBean=new LimsMethodCertificateBean();
                certificateBean.setId(ApplicationContextHelper.getNextIdentity());
                certificateBean.setPersonId(personId);
                certificateBean.setMethodId(d);
                certificates.add(certificateBean);
            });
            getDao().insert(certificates);
            return certificates.get(0).getId();
        }else {
            List<Long> methodIdList1=certificateBeans.stream().map(LimsMethodCertificateBean::getMethodId).distinct().collect(Collectors.toList());
            Collection<Long> diff= CollectionUtils.diff(methodIdList, methodIdList1);
            if(diff.isEmpty()){//不存在不相同的
                return 0L;
            }else {
                diff.stream().forEach(d ->{
                    LimsMethodCertificateBean certificateBean=new LimsMethodCertificateBean();
                    certificateBean.setId(ApplicationContextHelper.getNextIdentity());
                    certificateBean.setPersonId(personId);
                    certificateBean.setMethodId(d);
                    certificates.add(certificateBean);
                });
                getDao().insert(certificates);
                return certificates.get(0).getId();
            }

        }

    }

    @Override
    public void updateDate(RestJsonWrapperBean jsonWrapperBean) {
        List<LimsMethodCertificateBean> limsMethodCertificates = jsonWrapperBean.parse(LimsMethodCertificateBean.class);
        if(limsMethodCertificates.isEmpty()){
            return;
        }
        getDao().update(limsMethodCertificates,"expdate","certdate");
    }
}
